<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="客户名称" prop="customer">
				<el-input v-model="form.customer" placeholder="客户名称" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="政策类型" prop="policyType">
						<el-select v-model="form.policyType" clearable filterable style="width: 100%">
							<el-option v-for="item in policyTypeList" :key="item.id" :label="item.policyName" :value="item.id"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="审批状态" prop="applyStatus">
						<el-select v-model="form.applyStatus" clearable filterable style="width: 100%">
							<el-option v-for="item in applyStatusList" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="申请时间" prop="applyTime">
						<el-date-picker v-model="form.applyTime" placeholder="申请时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
				</el-col>
			</el-row>
			<el-form-item label="审批人" prop="applyUser">
				<select-user-dialog v-model:accountId="form.applyUser" :mode="false"/>
			</el-form-item>
			<el-form-item label="申请内容" prop="applyContent" style="margin-top: 10px;">
				<el-input type="textarea" rows="4" v-model="form.applyContent" placeholder="申请内容" clearable maxlength="380"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			loading:false,
			userList:[],
			mode: "add",
			apiObj:this.$API.file.attach.uploadFile,
			titleMap: {
				add: '新建申请',
				edit: '编辑申请',
			},
			form: {
				id:'',
				customer:'',
				policyApply:"",
				applyStatus: '',
				applyTime: '',
				applyUser: '',
				applyContent:''
			},
			policyTypeList:[]
			,applyStatusList:[{"label":"审批中","value":1},{"label":"已通过","value":2},{"label":"未通过","value":3}]
			,rules: {
				customer: [
					{required: true, message: '客户名称不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getCrmApprovedUserList();
		this.getPolicyTypeList();
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.policy.policyApply.savePolicyApply.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.policy.policyApply.updatePolicyApply.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form = data
		},
		async getPolicyTypeList()
		{
			let res = await this.$API.policy.policyType.getPolicyTypeList.get();
			if (res.code == 200) {
				this.policyTypeList = res.data; 
			}
		},
		async getCrmApprovedUserList()
		{
			let res = await this.$API.crm.crmRole.getCrmApprovedUserList.get();
			if (res.code == 200) {
				this.userList = res.data;
			}
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
